﻿package org.nickro.project.widgets{
    import flash.events.*;

    public class ToggleButtonSet {

        private var pOnButton:ToggleButton;
        private var pButtonLookup:Object;
        private var pEventType:String;
        private var pTrackAsMenu:Boolean;
        private var pToggleWhenClicked:Boolean;
        private var pButtons:int;
        private var pEventListener:Function;

        public static const CLICK_PUSH_BUTTON:String = "clickPushButton";

        public function ToggleButtonSet(toggleWhenClicked:Boolean=true, trackMenu:Boolean=true, eventListener:Function=null, eventType:String="clickPushButton"){
            super();
            pToggleWhenClicked = toggleWhenClicked;
            pTrackAsMenu = trackMenu;
            pButtonLookup = new Object();
            pButtons = 0;
            pEventListener = eventListener;
            pEventType = ((eventType)==null) ? PushButton.CLICK_PUSH_BUTTON : eventType;
        }
        public function getSelectedButtonID():String{
            return (((pOnButton is ToggleButton)) ? pOnButton.pID : null);
        }
        public function addToSet(oBtn:ToggleButton, ID=null, label:String=null):void{
			trace('Debug ::'+ oBtn)
            if (ID == null){
                ID = ("__BUTTON_" + pButtons);
            } else {
                oBtn.pID = String(ID);
            };
            if (label != null){
                oBtn.setLabel(label);
            };
            oBtn.pToggleWhenClicked = pToggleWhenClicked;
            if (pTrackAsMenu){
                oBtn.setTrackAsMenu(true);
            };
            if (pEventListener != null){
                oBtn.addEventListener(pEventType, pEventListener);
            };
            oBtn.addEventListener(CLICK_PUSH_BUTTON, buttonClickListener, false, 1);
            pButtonLookup[ID] = oBtn;
            pButtons++;
        }
        public function getSelectedButton():ToggleButton{
            return (pOnButton);
        }
        public function getButtonWithID(ID:String):ToggleButton{
            return (((ID)==null) ? null : pButtonLookup[ID]);
        }
        public function selectButtonWithID(ID:String):void{
            setSelectedButton(getButtonWithID(ID));
        }
        public function buttonClickHandler(oButton:ToggleButton):void{
            if (oButton == null){
                setSelectedButton(null);
            } else {
                oButton.clickHandler();
            };
        }
        private function setSelectedButton(oButton:ToggleButton):void{
            if (pOnButton != null){
                pOnButton.setIsOn(false);
                pOnButton.enable(true);
            };
            pOnButton = oButton;
            if (oButton != null){
                pOnButton.setIsOn(true);
                pOnButton.enable(false, false);
            };
        }
        private function buttonClickListener(e:Event):void{
            setSelectedButton(ToggleButton(e.target));
        }
        public function hasSelection():Boolean{
            return ((pOnButton is ToggleButton));
        }
        public function clickButtonWithID(ID:String):void{
            buttonClickHandler(getButtonWithID(ID));
        }

    }
}//package com.tobyfreeman.widgets 
